﻿
using System.Collections;
using System.Web.UI;
namespace visinia.SmartEngine.Modules
{
    /// <summary>
    /// The BaseModule is the backend of every visinia module
    /// visinia modules actually are not derived from this class,
    /// it actually behaves as a bridge between the actual module UserControl
    /// and the module settings UserControl to share module specific settings aka module private settings.
    /// Settings are shared through ModuleId property.
    /// 
    /// </summary>
    public class BaseModule : UserControl
    {
        private int _ModuleId;
        //private Hashtable _Settings;
        /// <summary>
        /// Id of this module
        /// </summary>
        public int ModuleId
        {
            get
            {
                return _ModuleId;
            }
            set
            {
                _ModuleId = value;
            }
        }

        /// <summary>
        /// Module private settings
        /// </summary>
        public Hashtable Settings
        {
            get
            {
                return ModuleController.GetModulePrivateSettings(ModuleId);
            }
        }

        /// <summary>
        /// Adds a new private setting
        /// </summary>
        /// <param name="key">The name of the setting</param>
        /// <param name="value">Value of the setting</param>
        public void AddSetting(string key, string value)
        {
            if (Settings == null)
            {
                ModuleController.AddModulePrivateSetting(ModuleId, key, value);
                return;
            }
            if (Settings.ContainsKey(key))
                ModuleController.UpdateModulePrivateSetting(ModuleId, key, value);
            else
                ModuleController.AddModulePrivateSetting(ModuleId, key, value);
        }

        /// <summary>
        /// Deletes the specified module private setting
        /// </summary>
        /// <param name="key">The name of the key to delete</param>
        public void DeleteSetting(string key)
        {
            if (Settings == null)
                return;
            if (Settings.ContainsKey(key))
                ModuleController.DeleteModulePrivateSetting(ModuleId, key);
        }
    }
}
